package demo1;

public class MyQueue {
    public static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    //入队
    public boolean offer(int data) {
        ListNode node = new ListNode(data);
        if(isEmpty()) {
            last = head = node;
        } else {
            last.next = node;
            node.prev = last;
            last = node;
        }
        return true;
    }

    public boolean isEmpty() {
        return head == null;
    }

    //出队
    public int poll() {
        if(isEmpty()) {
            return -1;
        }
        int ret = head.val;
        if(head.next != null) {
            head.next.prev = null;
        }
        head = head.next;
        return ret;
    }

    //求结点个数
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //获取队头元素
    public int peek() {
        if(isEmpty()) {
            return -1;
        }
        return head.val;
    }
}
